<?php
namespace app\admin\controller;
use think\Db;
class Replayimgtext extends Base
{   
    public function lists(){
        
        //ajax查询数据
        if( is_post() ){
            $page     = input('page');
            $type     = input('type');
            $status   = input('status');
            $keyword  = input('keyword');
            
            //原生态sql查询
            $sql = "
                select
                    [1]
                from
                    wx_replay_imgtext
                where 
                    1 = ?
                ";
            $param = [1];
            if ( $type != '100all' ) {
                $sql .= " and type = ?";
                $param[] = $type;
            }
            if ( $status != '100all' ) {
                $sql .= " and a.status = ?";
                $param[] = $status;
            }
            if ( !empty($keyword) ) {
                $sql .= " and keyword like ?";
                $param[] = "%{$keyword}%";
            }
        
            //总条数
            $sql_count = str_replace("[1]", "count(*) as total", $sql);
            $total     = Db::query($sql_count, $param);
            $total     = $total[0]['total'];
            
            //分页数据
            $page_size   = c('page_size');     #分页条数
            $page        = com_cur_page($total, $page,  $page_size);   #当前页
			$page_data   = com_page_data($total, $page,  $page_size);   #分页原始数据
            
            //列表数据
			$start       = ($page - 1) * $page_size;
			$sql        .= " order by id desc limit $start, $page_size";
			$field       = " * ";
			$sql_list    = str_replace("[1]", $field, $sql);
            $list        = Db::query($sql_list, $param);
            
            //格式数据
            foreach($list as $k => $v){                
                $list[$k]['checkbox']       =  com_format_checkbox($v['id']);

                $list[$k]['picurl']            =  com_format_pic(com_get_pic($v['picurl'],'small'));
                
                $list[$k]['start_time']    =  com_format_time( $v['start_time'] );
                $list[$k]['type']         =  c("type.rk_type" , $v['type']);
                $list[$k]['sort']           =  com_format_input_edit(url('ajax/editSort', 't=wx_replay_imgtext'), $v['id'], $v['sort']);
                
                $list[$k]['status']         =  com_format_status(url('ajax/editStatus', 't=wx_replay_imgtext'),$v['id'],$v['status']);

                //操作按钮
                $list[$k]['action']         =  com_action_edit( url("replayimgtext/edit","id={$v['id']}"), $v['id'], '编辑回复' );
                $list[$k]['action']        .=  com_action_del( url("ajax/delete","t=wx_replay_imgtext"), $v['id'], "你确定要删除{$v['keywords']}吗？" );
                
                
            }
			$com_body    = com_build_table($list, array("checkbox","id","picurl","keywords","title","description","url", "type", "sort", "status","send_times","start_time","action"));
			$com_page    = com_build_page( $page_data );
			$data = array(
				"com_body"  => $com_body,
				"com_page"  => $com_page,
			);
            $config = [
                'data'   => $data,
                'suc'    => [
                    'js_action' => 'function.switchs',
                ],
            ];
			com_check($config);
        }        
        
        $data = [
            'type'      => ['100all'=>'全部匹配'] + c('type.rk_type'),
            'status'    => ['100all'=>'全部状态'] + c('type.status'),
            'nav'       => ['文章管理', '列表'],
        ];
        return view('', $data);
    }
    


    //添加
    public function add(){
        if( is_post() ){
            $data = input();  #post数据
            unset($data['picurl']);

            //文件上传
            if( isset($_FILES['picurl']) ){
                $pic = com_up_pic('article', 'picurl', true);
                com_check( ['err'=>['js_action'=>'alert'], 'suc'=>['return_type'=>'return']]);
                $data['picurl'] = $pic;
            }

            $data['start_time'] = time();
            $ReplayImgText  = new \app\admin\model\ReplayImgText();
            $ReplayImgText->save($data);
            $config = [
                'msg' => '添加图文回复成功！',
                'suc' => [
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }    
        //模板赋值
        $data = [
            'type'      => c('type.rk_type'),
            'status'    => c('type.status'),
        ];
        return view('', $data);
    }
    
    
    //编辑
    public function edit( $id ){
        if( is_post() ){
            $data = input();  #post数据
            unset($data['picurl']);

            if( isset($_FILES['picurl']) ){
                $pic = com_up_pic('article', 'picurl', true);
                $data['picurl'] = $pic;
                com_check(['err'=>['js_action'=>'alert'], 'suc'=>['return_type'=>'return']] );
            }
            if( isset($data['delete_pic']) ){
                unset($data['delete_pic']);
                if(!isset($_FILES['picurl'])) $data['picurl'] = '';

            }
            
            $data['id'] = $id;
            \app\admin\model\ReplayImgText::update($data);
            $config = [
                'suc' => [
                    'msg' => '编辑图文回复成功！',
                    'js_action' => 'alert|close',
                ],
            ];
            com_check( $config );
        }
        $info   = \app\admin\model\ReplayImgText::get($id)->getData();
        $data = [
            'type'      => c('type.rk_type'),
            'status'    => c('type.status'),
            'info'      => $info,
        ];
        return view('', $data);
    }
    
    
    //批量删除
    public function delall(){
        if( is_post() ){
            \app\admin\model\ReplayImgText::destroy( implode(",", input('ids/a')) );
            $config = [
                'suc' => [
                    'msg' => '删除成功！',
                    'js_action'=>"alert|reload",
                ],
            ];
            com_check($config);
        }
    }
}